package com.sojson.project.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.enums.core.EGeneralStatus;
import com.sojson.project.entity.po.Brokerage;
import com.sojson.project.service.BrokerageService;
import com.sojson.result.Result;
import com.sojson.util.StringUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 分佣信息控制器
 * 
 * @author liu
 * @date 2021-06-29
 */
@Api(tags = "分佣信息管理")
@RestController
@RequestMapping("/brokerage")
public class BrokerageController {

    @Autowired
    private BrokerageService brokerageService;

    /**
     * 单条插入
     * 
     * @param brokerage
     * @return
     */
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('/brokerage/insert')")
    @ApiOperation("给分佣信息添加一条数据")
    public Result<Integer> insert(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.insert(brokerage));
    }

    /**
     * 批量插入
     * 
     * @param brokerage
     * @return
     */
    @PostMapping("/insertList")
    @PreAuthorize("@ss.hasPermi('/brokerage/insertList')")
    @ApiOperation("给分佣信息添加多条数据")
    public Result<Integer> insertList(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.insertList(brokerage));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param brokerage
     * @return
     */
    @DeleteMapping("/deleteByDto")
    @PreAuthorize("@ss.hasPermi('/brokerage/deleteByDto')")
    @ApiOperation("通过对象批量删除分佣信息数据(物理删除)")
    public Result<Integer> deleteByDto(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.deleteByDto(brokerage));
    }

    /**
     * 统计全部数量
     * 
     * @return
     */
    @GetMapping("/countAll")
    @PreAuthorize("@ss.hasPermi('/brokerage/countAll')")
    @ApiOperation("统计分佣信息全部数据(包含逻辑删除的)")
    public Result<Integer> countAll() {
        return Result.success(brokerageService.countAll());
    }

    /**
     * 查询所有
     * 
     * @return
     */
    @GetMapping("/listAll")
    @PreAuthorize("@ss.hasPermi('/brokerage/listAll')")
    @ApiOperation("查询分佣信息所有数据(包含逻辑删除的)")
    public Result<List<Brokerage>> listAll() {
        return Result.success(brokerageService.listAll());
    }

    /**
     * 根据对象统计数量
     * 
     * @param brokerage
     * @return
     */
    @GetMapping("/countByDto")
    @PreAuthorize("@ss.hasPermi('/brokerage/countByDto')")
    @ApiOperation("根据对象统计分佣信息数据")
    public Result<Integer> countByDto(Brokerage brokerage) {
        return Result.success(brokerageService.countByDto(brokerage));
    }

    /**
     * 根据对象查询
     * 
     * @param brokerage
     * @return
     */
    @GetMapping("/listByDto")
    @PreAuthorize("@ss.hasPermi('/brokerage/listByDto')")
    @ApiOperation("根据对象查询分佣信息数据")
    public Result<List<Brokerage>> listByDto(Brokerage brokerage) {
        return Result.success(brokerageService.listByDto(brokerage));
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    @PreAuthorize("@ss.hasPermi('/brokerage/deleteById/{id}')")
    @ApiOperation("通过id删除分佣信息数据(物理删除)")
    public Result<Integer> deleteById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(brokerageService.deleteById(id));
    }

    /**
     * 通过id数组删除(物理删除)
     * 
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/brokerage/deleteByIds')")
    @ApiOperation("通过id数组删除分佣信息数据(物理删除)")
    public Result<Integer> deleteByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(brokerageService.deleteByIds(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param brokerage
     * @return
     */
    @PutMapping("/updateAllById")
    @PreAuthorize("@ss.hasPermi('/brokerage/updateAllById')")
    @ApiOperation("通过id更新分佣信息数据(全部更新)")
    public Result<Integer> updateAllById(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.updateAllById(brokerage));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param brokerage
     * @return
     */
    @PutMapping("/updateAllByIdList")
    @PreAuthorize("@ss.hasPermi('/brokerage/updateAllByIdList')")
    @ApiOperation("通过id列表更新分佣信息数据(全部更新)(更新内容相同)")
    public Result<Integer> updateAllByIdList(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.updateAllByIdList(brokerage));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param brokerage
     * @return
     */
    @PutMapping("/updateDtoListAllById")
    @PreAuthorize("@ss.hasPermi('/brokerage/updateDtoListAllById')")
    @ApiOperation("通过id列表更新分佣信息数据(全部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListAllById(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.updateDtoListAllById(brokerage));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param brokerage
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/brokerage/updateById')")
    @ApiOperation("通过id更新分佣信息数据(局部更新)")
    public Result<Integer> updateById(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.updateById(brokerage));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param brokerage
     * @return
     */
    @PutMapping("/updateByIdList")
    @PreAuthorize("@ss.hasPermi('/brokerage/updateByIdList')")
    @ApiOperation("通过id列表更新分佣信息数据(局部更新)(更新内容相同)")
    public Result<Integer> updateByIdList(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.updateByIdList(brokerage));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param brokerage
     * @return
     */
    @PutMapping("/updateDtoListById")
    @PreAuthorize("@ss.hasPermi('/brokerage/updateDtoListById')")
    @ApiOperation("通过id列表更新分佣信息数据(局部更新)(更新内容不相同)")
    public Result<Integer> updateDtoListById(@RequestBody Brokerage brokerage) {
        return Result.success(brokerageService.updateDtoListById(brokerage));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/findById/{id}")
    @PreAuthorize("@ss.hasPermi('/brokerage/findById/{id}')")
    @ApiOperation("通过id查询分佣信息数据")
    public Result<Brokerage> findById(@PathVariable Integer id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(brokerageService.findById(id));
    }

    /**
     * 通过id数组查询
     * 
     * @param ids
     * @return
     */
    @GetMapping("/findByIds/{ids}")
    @PreAuthorize("@ss.hasPermi('/brokerage/findByIds/{ids}')")
    @ApiOperation("通过id数组查询分佣信息数据")
    public Result<List<Brokerage>> findByIds(@PathVariable Integer[] ids) {
        if (ids.length == 0) {
            return Result.success();
        }

        return Result.success(brokerageService.findByIds(ids));
    }

    /**
     * 通过对象查询id列表
     * 
     * @param brokerage
     * @return
     */
    @GetMapping("/findIdByDto")
    @PreAuthorize("@ss.hasPermi('/brokerage/findIdByDto')")
    @ApiOperation("通过对象查询分佣信息id列表")
    public Result<List<Integer>> findIdByDto(Brokerage brokerage) {
        return Result.success(brokerageService.findIdByDto(brokerage));
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     */
    @DeleteMapping("/clear")
    @PreAuthorize("@ss.hasPermi('/brokerage/clear')")
    @ApiOperation("清空分佣信息数据(物理清空)")
    public Result<Integer> clear() {
        return Result.success(brokerageService.clear());
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws IOException 
     */
    @GetMapping("/getInfo")
    @PreAuthorize("@ss.login()")
    public Object getInfo() throws IOException {
        return Result.success(brokerageService.getInfo());
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param brokerage
     * @return
     * @throws IOException 
     */
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('/brokerage/update')")
    public Object update(@RequestBody Brokerage brokerage) throws IOException {
        Brokerage param = new Brokerage();
        Integer status = brokerage.getStatus();
        if (StringUtil.isNotBlankObject(status) && status == EGeneralStatus.DISABLE.getCode()) {
            param = new Brokerage();
            param.setStatus(EGeneralStatus.DISABLE.getCode());
        } else {
            param = brokerage;
        }
        return Result.success(brokerageService.update(param));
    }

}